package parser;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 
 * Class Logger will help to find the different errors and violations encountered by the parsers to know in detail what happened
 *
 */
public class Logger {
	private String result;
	private File logFile;
	
	public Logger(String fileName) {
		logFile=new File(fileName);
		result=new String();
		result="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<files>\n";
	}
	
	public void setLogFileName(String fileName) {
		logFile = new File(fileName);
	}
	
	public File getLogFile() {
		return logFile;
	}
	
	public void addNewFileEntry(String file)
	{
		result+="<file name=\""+file+"\">\n";
	}
	
	public void addViolation(String type,String elementName)
	{
		result+="\t<violation type=\""+type+"\" element=\""+elementName+"\" />\n";
	}
	
	public void addException(String exception)
	{
		if (exception!=null)
		{
			exception.replace("<", "INF");
			exception.replace(">", "SUP");
			exception.replace("\"","''");
		}
		result+="<exception>"+exception+"</exception>\n";
	}
	
	public void closeFileEntry()
	{
		result+="</file>\n";
	}
	
	public void closeAll()
	{
		result+="</files>\n";
	}
	
	public void addInfo(String element,String content)
	{
		result+="<"+element+">\n"+content+"\n</"+element+">\n";
	}
	public void flush()
	{
		try {
			FileWriter fw=new FileWriter(logFile);
			fw.write(result);
			fw.close();
		}
		catch (IOException i)
		{
			i.printStackTrace();
		}
	}
}
